﻿namespace CardManagement
{
    using CardManagement.Language;
    using CardManagement.Properties;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class AddDevice : Form
    {
        private IContainer components = null;
        private Panel panel1;
        private Label label1;
        private Button btnCancel;
        private Button btnAdd;
        private TextBox textBoxPassword;
        private Label label5;
        private Label label3;
        private TextBox textBoxPort;
        private TextBox textBoxUserName;
        private Label label4;
        private Label label2;
        private TextBox textBoxDeviceAddress;

        public AddDevice(SysConfig config)
        {
            this.InitializeComponent();
            this.cfg = config;
        }

        private void AddDevice_Load(object sender, EventArgs e)
        {
            MultiLanguage.LoadLanguage(this);
            this.textBoxDeviceAddress.Text = this.cfg.DeviceIP;
            this.textBoxPort.Text = this.cfg.Port.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if ((this.textBoxDeviceAddress.Text.Length <= 0) || (this.textBoxDeviceAddress.Text.Length > 0x80))
            {
                MessageBox.Show(Resources.deviceAddressTips);
            }
            else
            {
                int num;
                int.TryParse(this.textBoxPort.Text, out num);
                if ((this.textBoxPort.Text.Length > 5) || (num <= 0))
                {
                    MessageBox.Show(Resources.portTips);
                }
                else if ((this.textBoxUserName.Text.Length > 0x20) || (this.textBoxPassword.Text.Length > 0x10))
                {
                    MessageBox.Show(Resources.usernameAndPasswordTips);
                }
                else
                {
                    this.Login();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(AddDevice));
            this.panel1 = new Panel();
            this.label1 = new Label();
            this.btnCancel = new Button();
            this.btnAdd = new Button();
            this.textBoxPassword = new TextBox();
            this.label5 = new Label();
            this.label3 = new Label();
            this.textBoxPort = new TextBox();
            this.textBoxUserName = new TextBox();
            this.label4 = new Label();
            this.label2 = new Label();
            this.textBoxDeviceAddress = new TextBox();
            base.Load += new EventHandler(this.AddDevice_Load);
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.panel1.BackgroundImage = (Image) manager.GetObject("panel1.BackgroundImage");
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x229, 0x52);
            this.panel1.TabIndex = 1;
            this.label1.Anchor = AnchorStyles.Left;
            this.label1.AutoSize = true;
            this.label1.BackColor = Color.Transparent;
            this.label1.Font = new Font("Consolas", 26.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.label1.ForeColor = Color.White;
            this.label1.Location = new Point(0xa7, 0x13);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0xf6, 0x29);
            this.label1.TabIndex = 2;
            this.label1.Text = "Login Device";
            this.btnCancel.FlatStyle = FlatStyle.Flat;
            this.btnCancel.Location = new Point(0x13a, 0xfd);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x67, 0x1d);
            this.btnCancel.TabIndex = 0x3f;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnAdd.FlatStyle = FlatStyle.Flat;
            this.btnAdd.Location = new Point(0x83, 0xfd);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new Size(0x67, 0x1d);
            this.btnAdd.TabIndex = 0x3e;
            this.btnAdd.Text = "Add";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new EventHandler(this.btnAdd_Click);
            this.textBoxPassword.Font = new Font("Consolas", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.textBoxPassword.Location = new Point(0xe1, 0xc6);
            this.textBoxPassword.Name = "textBoxPassword";
            this.textBoxPassword.Size = new Size(250, 0x16);
            this.textBoxPassword.TabIndex = 60;
            this.textBoxPassword.Text = "hik12345";
            this.textBoxPassword.UseSystemPasswordChar = true;
            this.label5.AutoSize = true;
            this.label5.BackColor = Color.Transparent;
            this.label5.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.label5.ForeColor = Color.DimGray;
            this.label5.Location = new Point(0x4f, 0xcb);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x48, 0x11);
            this.label5.TabIndex = 0x3d;
            this.label5.Text = "Password";
            this.label3.AutoSize = true;
            this.label3.BackColor = Color.Transparent;
            this.label3.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.label3.ForeColor = Color.DimGray;
            this.label3.Location = new Point(0x4f, 0xa8);
            this.label3.Name = "label3";
            this.label3.Size = new Size(40, 0x11);
            this.label3.TabIndex = 0x3b;
            this.label3.Text = "Port";
            this.textBoxPort.Location = new Point(0xe1, 0xa7);
            this.textBoxPort.Name = "textBoxPort";
            this.textBoxPort.Size = new Size(250, 0x16);
            this.textBoxPort.TabIndex = 0x3a;
            this.textBoxPort.Text = "8000";
            this.textBoxUserName.Location = new Point(0xe1, 0x87);
            this.textBoxUserName.Name = "textBoxUserName";
            this.textBoxUserName.Size = new Size(250, 0x16);
            this.textBoxUserName.TabIndex = 0x38;
            this.textBoxUserName.Text = "admin";
            this.label4.AutoSize = true;
            this.label4.BackColor = Color.Transparent;
            this.label4.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.label4.ForeColor = Color.DimGray;
            this.label4.Location = new Point(0x4f, 0x88);
            this.label4.Name = "label4";
            this.label4.Size = new Size(80, 0x11);
            this.label4.TabIndex = 0x39;
            this.label4.Text = "User Name";
            this.label2.AutoSize = true;
            this.label2.BackColor = Color.Transparent;
            this.label2.Font = new Font("Consolas", 10.5f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.label2.ForeColor = Color.DimGray;
            this.label2.Location = new Point(0x4f, 0x69);
            this.label2.Name = "label2";
            this.label2.Size = new Size(120, 0x11);
            this.label2.TabIndex = 0x37;
            this.label2.Text = "Device Address";
            this.textBoxDeviceAddress.Location = new Point(0xe1, 0x68);
            this.textBoxDeviceAddress.Name = "textBoxDeviceAddress";
            this.textBoxDeviceAddress.Size = new Size(250, 0x16);
            this.textBoxDeviceAddress.TabIndex = 0x36;
            this.textBoxDeviceAddress.Text = "10.7.113.150";
            base.AutoScaleDimensions = new SizeF(7f, 14f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x229, 0x141);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnAdd);
            base.Controls.Add(this.textBoxPassword);
            base.Controls.Add(this.label5);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.textBoxPort);
            base.Controls.Add(this.textBoxUserName);
            base.Controls.Add(this.label4);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.textBoxDeviceAddress);
            base.Controls.Add(this.panel1);
            this.Font = new Font("Consolas", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.Name = "AddDevice";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Login Device";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public void Login()
        {
            if (MainForm.m_UserID >= 0)
            {
                CHCNetSDK.NET_DVR_Logout_V30(MainForm.m_UserID);
                MainForm.m_UserID = -1;
            }
            CHCNetSDK.NET_DVR_USER_LOGIN_INFO pLoginInfo = new CHCNetSDK.NET_DVR_USER_LOGIN_INFO();
            CHCNetSDK.NET_DVR_DEVICEINFO_V40 lpDeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V40 {
                struDeviceV30 = { sSerialNumber = new byte[0x30] }
            };
            pLoginInfo.sDeviceAddress = this.textBoxDeviceAddress.Text;
            pLoginInfo.sUserName = this.textBoxUserName.Text;
            pLoginInfo.sPassword = this.textBoxPassword.Text;
            ushort.TryParse(this.textBoxPort.Text, out pLoginInfo.wPort);
            int num = -1;
            num = CHCNetSDK.NET_DVR_Login_V40(ref pLoginInfo, ref lpDeviceInfo);
            if (num >= 0)
            {
                MainForm.m_UserID = num;
                MessageBox.Show("Login Successful");
                base.Close();
            }
            else
            {
                uint num2 = CHCNetSDK.NET_DVR_GetLastError();
                if (num2 == 1)
                {
                    MessageBox.Show("user name or password error!");
                    if (1 == lpDeviceInfo.bySupportLock)
                    {
                        MessageBox.Show($"Left {lpDeviceInfo.byRetryLoginTime} try opportunity");
                    }
                }
                else if (num2 != 0x99)
                {
                    MessageBox.Show("net error or dvr is busy!");
                }
                else if (1 == lpDeviceInfo.bySupportLock)
                {
                    MessageBox.Show($"user is locked, the remaining lock time is {lpDeviceInfo.dwSurplusLockTime}");
                }
            }
        }

        public SysConfig cfg { get; set; }
    }
}

